import java.util.List;

class ListNode{
    public int val;
    public ListNode prev;
    public ListNode next;

    public ListNode(int val){
        this.val = val;
    }

}


public class MyLinkedList {
    public ListNode head;//指向双向链表的头结点
    public ListNode last;//指向双向链表的尾节点

    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){
        ListNode cur = this.head;
        int cnt = 0;
        while(cur != null){
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
        }else{
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }

    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            this.head = node;
            this.last = node;
        }else{
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }
    public ListNode searchIndex(int index){
        ListNode cur = this.head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void addIndex(int index,int data){
        ListNode node = new ListNode(data);
        if(index < 0 || index > size()){
            System.out.println("位置不合法");
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode cur = searchIndex(index);
        node.next = cur.prev.next;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    public void removeList(int key){
        ListNode cur = this.head;
        if(cur == null){
            System.out.println("该单链表不存在");
            return;
        }
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    this.head = this.head.next;
                    if(head != null){
                        this.head.prev = null;
                    }else{
                        last = null;
                    }

                } else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        last = last.prev;
                    }
                }
                return;
            }
            cur = cur.next;

        }
    }


    public void removeAllKey(int key){
        ListNode cur = this.head;
        if(cur == null){
            System.out.println("该单链表不存在");
            return;
        }
        while(cur != null){
            if(cur.val == key){
                if(cur == head){
                    this.head = this.head.next;
                    if(head != null){
                        this.head.prev = null;
                    }else{
                        last = null;
                    }

                } else{
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        last = last.prev;
                    }
                }
                //return;
            }
            cur = cur.next;
        }
    }

    public void clear(){
        while(this.head != null){
            ListNode curNext = this.head.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = curNext;
        }
        last = null;
    }
}
